package ext.tianma.part.statute;

import java.io.File;
import java.util.Hashtable;
import ext.com.iba.IBAUtil;
import ext.tianma.bom.util.ClassificationUtil;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.part.forms.TMTypeUtil;
import ext.tianma.util.IbaAttributeUtil;
import ext.tianma.util.PropertiesUtil;
import wt.csm.navigation.ClassificationNode;
import wt.fc.PersistenceHelper;
import wt.fc.WTObject;
import wt.part.WTPart;

/**
 * 用于零部件承认 是否需要零件承认（背光、铁框、TP、PCB（A）、FPC（A）需要零件承认）:是-否
 * 
 * @author laichao_sun
 *
 */

public class StatutePartsAdmit {
	public static String configFilePath = "codebase" + File.separator + "ext" + File.separator + "tianma"
			+ File.separator + "cfg" + File.separator + "TianmaPro.properties";

	public static boolean isPartsAdmit(WTObject object) {

		try {
			WTPart part = null;
			if (object instanceof WTPart) {
				part = (WTPart) object;
				if (TMTypeUtil.isRawMaterial(part)) {
					PropertiesUtil ut = new PropertiesUtil(configFilePath);
					String partCNandENnames = ut.getValueByKey("Part_Sample_name");
					System.out.println("SLC ==需要零件承认的中英文名称==>" + partCNandENnames);
					// 根据分类去判断是否需要零部件承认
					//ClassificationNode classificationNode = ClassificationUtil.getClassificationNode(part);
					//String name = classificationNode.getName();
					//System.out.println("Slc ==分类名称=>>" + classificationNode.getName());
					// 获取分类全路径
					// String classification = ClassificationUtil.getClassification(part);
					//中英文名称
					String name = IbaAttributeUtil.getAttributeValue(part, "partCNAndENName");
					if (StringUtil.isNullOrEmpty(name)) {
						return false;
					}
					boolean flag = partCNandENnames.contains(name);
					if (flag) {
						// 为零部件是否需要承认设置
						Hashtable<String, String> values = new Hashtable<String, String>();
						values = addToHashtable(values, "partRecognize", "是");
						IBAUtil.setSoftTypeIBAValues(part, values);
						PersistenceHelper.manager.refresh(part);
						return true;
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 实现Hashtable添加数据时不会出现空指针异常
	 * 
	 * @param hashtable
	 * @param key
	 * @param value
	 * @return
	 */
	private static Hashtable<String, String> addToHashtable(Hashtable<String, String> hashtable, String key,
			String value) {
		if (key == null && hashtable != null) {
			return hashtable;
		}

		if (value == null && hashtable != null) {
			return hashtable;
		}

		if (key == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (value == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (hashtable == null) {
			hashtable = new Hashtable<String, String>();
		}
		hashtable.put(key, value);

		return hashtable;
	}

}
